package com.tong.embeddedvr.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tong.embeddedvr.constant.ConstantPool;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class CommonUtil {

    /*
     *这里暂时先不考虑时间格式的转换
     * errCode
     * 200:正常返回
     * 300:因为一些特殊的原因而无法成功,如主机资源有限
     * 400:业务逻辑层面的错误
     * 500:系统错误
     */

    public static String ajaxReturn(Object data,Integer errCode,String errMsg) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String,Object> res = new HashMap<>();
        res.put("data",data);
        res.put("errCode",errCode);
        res.put("errMsg",errMsg);
        return objectMapper.writeValueAsString(res);
    }

    public static String ajaxReturn(Object data) {
        try {
            return ajaxReturn(data,200,"null");
        } catch (JsonProcessingException jsonProcessingException) {
            jsonProcessingException.printStackTrace();
            System.out.println("Jaskson转换错误:" + new Date());
            System.out.println("data:" + data);
            return ajaxReturn(500,"系统错误:请联系管理员");
        }
    }

    //特殊请款,手动返回JSON结果
    public static String ajaxReturn(Integer errCode,String errMsg){
        String res = "{\"data\":null,\"errCode\":" + errCode +",\"errMsg\":\"" + errMsg + "\"}";
        return res;
    }

    /**
     * @Description 非常实用的工具类
     * @Author gcl
     * @Date 2020/12/28 19:56
     * @param:
     * @Return java.lang.String
     */
    public static String getTime(){
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String res = sdf.format(new Date());
        return res;
    }

    public static String getTime(String date){
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String res = sdf.format(date);
        return res;
    }

    public static void sendEmail(MailProperties mailProperties,JavaMailSenderImpl mailSender,String email,String subject,String text){
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //读取配置文件中的邮件地址
        mailMessage.setFrom(mailProperties.getUsername());
        mailMessage.setTo(email);
        mailMessage.setSubject(subject);
        mailMessage.setText(text);
        mailSender.send(mailMessage);
    }

    //获得特定长度的随机字符串
    public static String getRandomString(int n){
        return getRandomString(n,ConstantPool.RAMDOM_ALL);
    }

    //获得特定长度的随机字符串
    public static String getRandomString(int n,String requires){
        String str = null;
        if (requires.equals(ConstantPool.RAMDOM_LOW_CASE)){
            str = "abcdefghijklmnopqrstuvwxyz";
        }else if(requires.equals(ConstantPool.RAMDOM_CAPITAL_TITLE)){
            str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        }else if (requires.equals(ConstantPool.RAMDOM_NUMBER)){
            str = "123456789";
        }else {
            str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }
        Integer len = str.length();
        Random random = new Random();
        StringBuffer ans = new StringBuffer();
        for(int i=1;i<=n;i++){
            int index = random.nextInt(len);
            ans.append(str.charAt(index));
        }
        return ans.toString();
    }

    //

    public static DefaultMapperFactory getMapperFactory(){
        return new DefaultMapperFactory.Builder().build();
    }

}
